package btcbot.entities;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import btcbot.communication.CommunicationManager;

import net.sf.json.JSONObject;

public class Account {

	private String id;
	private String login;
	private Date lastLogin;
	private Map<Currency, Wallet> walletMap;
	private Double tradeFee;

	public Account(JSONObject jsonAccount) throws ParseException {
		super();
		this.id = jsonAccount.getString("Id");
		this.login = jsonAccount.getString("Login");
		this.lastLogin = new SimpleDateFormat(CommunicationManager.DATE_PATTERN).parse(jsonAccount.getString("Last_Login"));
		this.tradeFee = jsonAccount.getDouble("Trade_Fee");
		this.walletMap = new HashMap<Currency, Wallet>();
		JSONObject jsonWallets = jsonAccount.getJSONObject("Wallets");
		for (Object key : jsonWallets.keySet()) {
			this.walletMap.put(Currency.valueOf((String)key), new Wallet(jsonWallets.getJSONObject((String)key)));
		}
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public Date getLastLogin() {
		return lastLogin;
	}

	public void setLastLogin(Date lastLogin) {
		this.lastLogin = lastLogin;
	}

	public Map<Currency, Wallet> getWalletMap() {
		return walletMap;
	}

	public void setWalletMap(Map<Currency, Wallet> walletMap) {
		this.walletMap = walletMap;
	}

	public Double getTradeFee() {
		return tradeFee;
	}

	public void setTradeFee(Double tradeFee) {
		this.tradeFee = tradeFee;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((lastLogin == null) ? 0 : lastLogin.hashCode());
		result = prime * result + ((login == null) ? 0 : login.hashCode());
		result = prime * result + ((tradeFee == null) ? 0 : tradeFee.hashCode());
		result = prime * result + ((walletMap == null) ? 0 : walletMap.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Account other = (Account) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (lastLogin == null) {
			if (other.lastLogin != null)
				return false;
		} else if (!lastLogin.equals(other.lastLogin))
			return false;
		if (login == null) {
			if (other.login != null)
				return false;
		} else if (!login.equals(other.login))
			return false;
		if (tradeFee == null) {
			if (other.tradeFee != null)
				return false;
		} else if (!tradeFee.equals(other.tradeFee))
			return false;
		if (walletMap == null) {
			if (other.walletMap != null)
				return false;
		} else if (!walletMap.equals(other.walletMap))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Account [#" + id + "; " + login + "; " + walletMap.toString() + "; lastLogin=" + lastLogin + "; tradeFee=" + tradeFee + "]";
	}

}
